package cn.itsource.pet.controller;

import cn.itsource.basic.util.AxajResult;
import cn.itsource.basic.util.PageInfo;
import cn.itsource.basic.util.SystemLoginInfo;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.query.PetQuery;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.IPetDetailService;
import cn.itsource.pet.service.IPetService;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.user.domain.LoginInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IPetService petService;
    @Autowired
    private IPetDetailService petDetailService;

    /**
     * 保存宠物信息
     *
     * @param pet
     * @param request
     * @return
     */
    @PostMapping
    public AxajResult save(@RequestBody Pet pet, HttpServletRequest request) {
        try {
            LoginInfo loginInfo = SystemLoginInfo.get(request);
            //获取员工对象
            Employee employee = employeeService.findByLoginInfoId(loginInfo.getId());
            //把宠物设置到店铺中
            pet.setShop(employee.getShop());
            //保存宠物对象
            petService.save(pet,employee);
            return new AxajResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxajResult(false, e.getMessage());
        }
    }

    @PatchMapping
    @ApiOperation(value = "分页查询宠物", notes = "很好用的接口")
    public PageInfo<Pet> findAll(@RequestBody PetQuery petQuery, HttpServletRequest request) {
        LoginInfo loginInfo = SystemLoginInfo.get(request);
        //通过登录用户获取employee对象
        Employee employee = employeeService.findByLoginInfoId(loginInfo.getId());
        //设置查询条件
        petQuery.setShopId(employee.getShop().getId());
        //分页查询
        return petService.findAllByQuery(petQuery);
    }

    /**
     * 批量上架
     *
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AxajResult onSale(@RequestBody List<Long> ids) {
        try {
            petService.batchOnSale(ids);
            return new AxajResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxajResult(false, e.getMessage());
        }
    }

    /**
     * 批量上架
     *
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AxajResult offSale(@RequestBody List<Long> ids) {
        try {
            petService.batchOffSale(ids);
            return new AxajResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxajResult(false, e.getMessage());
        }
    }

    //========================================门户网站代码==============================================
    @PatchMapping("/findHomePageByQuery")
    @ApiOperation(value = "门户网站分页查询宠物", notes = "很好用的接口")
    public PageInfo<Pet> findHomePageByQuery(@RequestBody PetQuery petQuery, HttpServletRequest request) {
        //分页查询
        return petService.findAllByQuery(petQuery);
    }


    /**
     * 通过id查询对应的宠物
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Pet findPetById(@PathVariable("id") Long id) {
        Pet pet = petService.findById(id);
        return pet;
    }

    /**
     * 立即领养接口
     *
     * @param id
     * @return
     */
    @PostMapping("/adopt/{id}")
    public AxajResult adopt(@PathVariable("id") Long id, HttpServletRequest request) {

        try {
            //获取登录信息用户
            LoginInfo loginInfo = SystemLoginInfo.get(request);
            petService.adopt(id, loginInfo.getId());
            return new AxajResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxajResult(false, e.getMessage());
        }
    }


    /**
     * 通过id删除宠物
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AxajResult delete(@PathVariable("id") Long id) {
        try {
            petService.delete(id);
            return new AxajResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxajResult(false,e.getMessage());
        }
    }

    /**
     * 根据宠物id获取宠物详情
     * @return
     */
    @GetMapping("/detail/{id}")
    public PetDetail loadDetailByPetId(@PathVariable("id") Long id){
        return petDetailService.findById(id);
    }

    @PostMapping("/dels")
    public AxajResult batchDels(@RequestBody List<Long> ids){
        try {
            petService.batchDels(ids);
            return new AxajResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxajResult(false,e.getMessage());
        }
    }

}
